package com.itheima.xixi.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.xixi.Mapper.CategoryMapper;
import com.itheima.xixi.Service.CategoryService;
import com.itheima.xixi.Service.DishService;
import com.itheima.xixi.Service.SetmealService;
import com.itheima.xixi.common.CustomException;
import com.itheima.xixi.entity.Category;
import com.itheima.xixi.entity.Dish;
import com.itheima.xixi.entity.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

   @Autowired
   private DishService dishService;

   @Autowired
   private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //查询当前分类是否关联了菜品，如果关联抛出业务异常
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        if (count1 > 0){
            //已经关联菜品，抛出异常
            throw new CustomException("当前分类下关联了菜品，不能删除！");
        }
        //查询当前分类是否关联了套餐，如果关联抛出业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper =new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);

        int count2 = setmealService.count();
        if (count2 > 0){
            //已经关联套餐，抛出一个业务异常
        }
        //正常删除分类
        super.removeById(id);
    }
}
